<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="shortcut icon" href="../static/images/local/校标.jpg" />
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../static/layui/css/layui.css"   media="all">
    <script src="../static/layui/layui.js" ></script>
    <link rel="stylesheet" href="../static/css/mySignIn.css"   media="all">
    <title>发布新闻</title>
</head>
<body>
<br>
<br>
<form class="layui-form" >
    <div class="layui-form-item">
        <label class="layui-form-label">新闻标题</label>
        <div class="layui-input-block">
            <label>
                <input type="text" id="title" name="title" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
            </label>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">概述</label>
        <div class="layui-input-block">
            <label>
                <input type="text"  id="dec" name="title" required  lay-verify="required" placeholder="概述" autocomplete="off" class="layui-input">
            </label>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">种类</label>
        <div class="layui-input-block">
            <input type="checkbox" name="like[dai]" title="娱乐" value="娱乐">
            <input type="checkbox" name="like[dai]" title="军事" value="军事">
            <input type="checkbox" name="like[dai]" title="体育" value="体育">
            <input type="checkbox" name="like[dai]" title="科技" value="科技">
            <input type="checkbox" name="like[dai]" title="生活" value="生活">
            <input type="checkbox" name="like[dai]" title="教育" value="教育">
            <input type="checkbox" name="like[dai]" title="游戏" value="游戏">
            <br>
            <input type="checkbox" name="like[dai]" title="国际" value="国际">
            <input type="checkbox" name="like[dai]" title="健康" value="健康">
            <input type="checkbox" name="like[dai]" title="旅游" value="旅游">
            <input type="checkbox" name="like[dai]" title="NBA" value="NBa">
            <input type="checkbox" name="like[dai]" title="财金" value="财金">
            <input type="checkbox" name="like[dai]" title="数码" value="数码">
            <input type="checkbox" name="like[dai]" title="汽车" value="汽车">
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">正文内容</label>
        <div class="layui-input-block">
            <label>
                <textarea name="desc" id="txt"  placeholder="请输入内容" class="layui-textarea"></textarea>
            </label>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn " lay-submit lay-filter="formDemo" type="button" onclick="newsAdd()">立即发布</button>
            <button type="reset" class=" layui-btn layui-btn-primary layui-border-red">重置</button>
        </div>
    </div>
</form>


<input type="hidden" name="images" class="image">
<div class="layui-form-item">
    <label class="layui-form-label ">附件图片:</label>
    <div class="layui-upload">
        <button type="button" class="layui-btn layui-btn layui-btn-normal " id="test1">选择图片</button>
        <button type="button" class="layui-btn" id="uploadButton">开始上传</button>
        <div class="layui-upload-list ">
            <img class="layui-upload-img img1" id="demo1"  >
        </div>
    </div>
</div>

<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<script src="http://cdn.staticfile.org/moment.js/2.24.0/moment.js"></script>
<script src="../static/js/moment.js"></script>
<script>
    const suffix=localStorage.getItem("suffix");
    const prefix=localStorage.getItem("prefix");
    if(sessionStorage.getItem("root")!=="1"&&sessionStorage.getItem("root")!=="0")
    {window.alert("请先登录!");window.location.href=prefix+"/logIn"+suffix;}
    $("#demo1").css("display",'none');// 隐藏边角线
    const baseUrl=localStorage.getItem("baseUrl");
    let path=""; //
    const user=JSON.parse(sessionStorage.getItem("user"));
    function newsAdd(){
        const id = $('input[type=checkbox]');
        let sort = '';
        id.each(function () {
            //获取当前元素的勾选状态
            if ($(this).prop("checked")) {
                sort = sort + $(this).val() + " ";
            }
        })
        console.log(moment().format('YYYY-MM-DD HH:mm:ss'));
        // console.log(sort);
        const news={
            newsTitle:document.getElementById("title").value,
            newsAuthor:user.userName,
            newsDesc:document.getElementById("dec").value,
            newsContent:document.getElementById("txt").value,
            newsSort:sort,
            newsDate:moment().format('YYYY-MM-DD HH:mm:ss'),
            newsImagesPath:path,
        }

        path=""//用完后归0
        if(check(news))
        {$.ajax({
            url: baseUrl+"/news/add_news",
            dataType: "json",
            type: "post",
            headers : {
                'Content-Type' : 'application/json;charset=utf-8'
            },
            data: JSON.stringify(news),
            success:function (res) {
                console.log(res)
                if (res) {alert("发布成功!");
                window.location.reload()}
                else layer.alert("发布失败!");
            },
            error: function ()
            {
                alert("服务器内部请求错误!");
            }
        })}
        else {

        }

    }
    layui.use('upload', function(){
        const $ = layui.jquery
            , upload = layui.upload;
        upload.render({
            elem: '#test1'
            , url: baseUrl+'/news/upload_file'
            , accept: 'images'
            , auto: false
            , size: 1024*100
            , bindAction: '#uploadButton'
            , before: function (obj) {//预览
                obj.preview(function (index, file, result) {
                    $("#demo1").css("display",'block');
                    $('#demo1').attr('src', result);
                });
            }
            , done: function (res) {//上传完后做的事
                if (res.code > 0) {
                    layer.alert('上传失败');
                }
                else{
                    layer.alert("上传成功");
                }
                  path=res.data.src;
                //console.log(path);
            }
            , error: function () {
                layer.alert("上传失败!");
            }
        });

    });
    function check(news) {
        if($.trim(news.newsTitle)==="")
        {
            layer.alert("请输入文章标题!");
            return false;
        }
        if($.trim(news.newsDesc)==="")
        {
            layer.alert("请输入文章简述!");
            return false;
        }
        if($.trim(news.newsSort)==="")
        {
            layer.alert("请输入选择种类!");
            return false;
        }
        if($.trim(news.newsContent)==="")
        {
            layer.alert("请输入文章内容!");
            return false;
        }
        return true;
    }
</script>
</body>
</html>